#pragma once
#include "log.hpp"
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>

#define PATHNAMW "./"
#define PROJ_ID 0x6676
#define IPC_MOD 0666

//创建共享内存空间
int commShm(int size,int flags){
    key_t key = ftok(PATHNAMW,PROJ_ID);
    if(key < 0){
        log("fatol","\"ftok\" is failed", __FILE__, __LINE__);
        exit(errno);
    }
    int shmid = shmget(key,size,flags);
    if(shmid < 0){
        log("fatol","\"shmget\" is failed", __FILE__,__LINE__);
        exit(errno);
    }
    return shmid;
}

void destoryShm(int shmid){
    if(-1 == shmctl(shmid,IPC_RMID,nullptr) == -1){
        log("Error","\"shmctl\" is failed", __FILE__,__LINE__);
        exit(errno);
    }
}

int creatShm(int size){
    return commShm(size,IPC_CREAT|IPC_EXCL|IPC_MOD);
}

int getShm(int size){
    return commShm(size,IPC_CREAT|IPC_MOD);
}




